package com.msmall.community.common.action.html;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.ValidateUtil;
import com.msmall.community.common.Configuration;
import com.msmall.community.common.OnlineUser;
import com.msmall.community.score.service.ScoreManager;
import com.msmall.community.user.model.LoginInfo;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;

public class RegisterAction extends HTMLBaseAction{

	private static final long serialVersionUID = -3417827961636127428L;
	private String account;
	private String password;
	private String nick;
	private Short gender;
	private String email;
	private String code; //验证码
	
	private UserManager userManager;
	
	private ScoreManager scoreManager;
	
	public String gr(){
		return SUCCESS;
	}
	
	public String register() {
		try {
			UserInfo userInfo = new UserInfo();
			userInfo.setUsername(account);
			userInfo.setNickname(nick);
			userInfo.setPassword(password);
			userInfo.setEmail(email);
			userInfo.setGender(Short.valueOf(gender));
			userManager.register(userInfo);
			
			
			//自动登录
			userInfo = userManager.getByUsername(account);
			OnlineUser.userAuthenticated(getUserid(), userInfo.getUserid(), getSession());
			
			// 注册添加积分
			addRegisterScore(userInfo);
			addLoginInfo(userInfo);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String validateAccount(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try {
			int maxlength = Configuration.getIntegerValue(Configuration.USER_USERNAME_LENGTH_MAX);
			int minlength = Configuration.getIntegerValue(Configuration.USER_USERNAME_LENGTH_MIN);
			if(account == null || "".equals(account.trim()) || account.length() < minlength){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.register.error.username.short")));
			}else if(account.length() > maxlength){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.register.error.username.long")));
			}else if(!ValidateUtil.validateUsername(account)){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.register.error.username.pattern")));
			}else{
				boolean ac = userManager.isUsernameExisted(account);
				if(!ac){
					buffer.append(createResult("true"));
				}else{
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.register.error.username.exist")));
				}
			}
		}catch(Exception e){
			buffer.append(createResult("false"));
			buffer.append(createEasyContent(getText("html.register.error.username.pattern")));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	public String validateNickname(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try {
			int maxlength = Configuration.getIntegerValue(Configuration.USER_NICKNAME_LENGTH_MAX);
			int minlength = Configuration.getIntegerValue(Configuration.USER_NICKNAME_LENGTH_MIN);
			if(nick == null || "".equals(nick.trim()) || nick.length() < minlength){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.register.error.nickname.short")));
			}else if(nick.length() > maxlength){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.register.error.nickname.long")));
			}else if(!ValidateUtil.validateNickname(nick)){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.register.error.nickname.pattern")));
			}else{
				buffer.append(createResult("true"));
			}
//			else{
//				UserInfo user = userManager.getByNickname(nick);
//				if(user == null){
//					buffer.append(createResult("true"));
//				}else if(user.getUserid().compareTo(getUserid()) == 0){
//					buffer.append(createResult("true"));
//				}else{
//					buffer.append(createResult("false"));
//					buffer.append(createEasyContent(getText("html.register.error.nickname.exist")));
//				}
//			}
		}catch(Exception e){
			buffer.append(createResult("false"));
			buffer.append(createEasyContent(getText("html.register.error.nickname.pattern")));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	
	public void addRegisterScore(UserInfo userInfo){
		scoreManager.addRegister(userInfo.getUserid(), false);
	}
	
	private void addLoginInfo(UserInfo user){
		LoginInfo login = new LoginInfo();
		login.setUserid(user.getUserid());
		login.setLoginUa(getUserAgentType());
		userManager.addLoginInfo(login);
	}
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setScoreManager(ScoreManager scoreManager) {
		this.scoreManager = scoreManager;
	}
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public Short getGender() {
		return gender;
	}
	public void setGender(Short gender) {
		this.gender = gender;
	}
}
